import 'package:flutter/material.dart';
import '../utils/debounce.dart';

enum ClickType {
  none,
  throttle,
  throttleWithTimeout,
  debounce,
}

class ClickWidget extends StatelessWidget {
  const ClickWidget({
    Key? key,
    this.onTap,
    required this.child,
    this.onDoubleTap,
    this.type = ClickType.none,
    this.timeout,
    this.clipBehavior = Clip.hardEdge,
    this.borderRadius,
    this.behavior,
  }) : super(key: key);

  final GestureTapCallback? onTap;
  final Widget child;
  final GestureTapCallback? onDoubleTap;
  final ClickType type;
  final int? timeout;
  final Clip clipBehavior;
  final BorderRadius? borderRadius;
  final HitTestBehavior? behavior;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: behavior ?? HitTestBehavior.opaque,
      onTap: _getOnTap(),
      onDoubleTap: onDoubleTap,
      child: child,
    );
  }

  GestureTapCallback? _getOnTap() {
    if (type == ClickType.throttle) {
      return onTap?.throttle();
    } else if (type == ClickType.throttleWithTimeout) {
      return onTap?.throttleWithTimeout(timeout: timeout);
    } else if (type == ClickType.debounce) {
      return onTap?.debounce(timeout: timeout);
    }
    return () => onTap?.call();
  }
}
